import moment from "moment";
export default {
  components: {
    zj_product_navs: resolve =>
      require(["./component/zj_product_navs"], resolve),
    paper_pre_model: resolve => require(["./component/model1.vue"], resolve),
    zj_page: resolve => require(["./component/zj_page"], resolve),
    zj_lx: resolve => require(["./component/zjlx.vue"], resolve),
    zn_lx: resolve => require(["./component/znlx.vue"], resolve),
    dayPractice: resolve => require(["./component/dayPractice.vue"], resolve),
    mDay: resolve => require(["../../page_cmm/moment.vue"], resolve)
  },
  data() {
    return {
      startExam: {
        //开始考试的时间&用于计时
        jTime: "2021-08-09 12:00:00",
        fTime: "2021-08-13 19:00:00",
        gTime: "2021-08-14 19:00:00",
        sTime: "2021-08-15 19:00:00",
        zTime: "2021-08-15 19:00:00"
      },
      isStart: {
        //是否已经开始考试
        showJ: false,
        showF: false,
        showG: false,
        showS: false,
        showZ: false
      },

      jsStartTime: {
        //计时器开始显示的事件&
        jTime: "2021-08-10 19:00:00",
        fTime: "2021-08-11 19:00:00",
        gTime: "2021-08-12 19:00:00",
        sTime: "2021-08-13 19:00:00",
        zTime: "2021-08-13 19:00:00"
      },
      //考试结束时间
      examEnd: {
        jTime: "2021-08-09 20:07:00",
        fTime: "2021-08-13 22:00:00",
        gTime: "2021-08-14 22:00:00",
        sTime: "2021-08-15 23:00:00",
        zTime: "2021-08-15 23:00:00"
      },
      showTime: {
        //控制计时器的显示和隐藏
        showJ: false,
        showF: false,
        showG: false,
        showS: false,
        showZ: false
      },
      remaining_time: 256321456,
      wrList: [],

      // 我要报名
      modal1: false,
      // 是否点击万人模考
      nowrmk: true,
      columns1: [
        {
          title: "试卷名称",
          key: "sj_name"
        },
        {
          title: "试卷年份",
          key: "year"
        },
        {
          title: "题目数量",
          key: "num"
        },
        {
          title: "试卷分数",
          key: "fz"
        },
        {
          title: "考试时长",
          key: "sc"
        },
        {
          title: "",
          slot: "action",
          width: 150,
          align: "center"
        }
      ],
      // 科目
      kmList: [],
      page_cmm: {
        // 页码
        page: 1,
        // 每页数据量
        size: 8,
        total: 0
      },
      router_params: {
        lb: "",
        kmlb: "",
        genre: "",
        sort: "new"
      }.merge_by_self_keys(this.$route.query),
      list_data: null,
      zjList: null,
      zjLists: null,
      znLists: ["1"],
      dayData: null,
      idStr: null,
      weekList: [],
      weekPaper: [],
      timer: null,
      examProp: false,
      // 开始时间以及科目
      tipObj: {
        tipTime: "",
        kmTip: ""
      },
      dayStr: "",
      nowTime: "" //服务器时间
    };
  },
  watch: {
    wrList: {
      handler(newdata) {
        console.log("数组发生了变化请及时查看");
        // this.$router.go(0);
      },
      deep: true
    }
  },
  computed: {
    text_remaining_time() {
      const text_time_info = this.remaining_time.toTime();
      return text_time_info.h + ":" + text_time_info.m + ":" + text_time_info.s;
    },
    day() {
      let week = [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六"
      ];
      let dObj = {};
      let d = new Date();
      let y = d.getFullYear();
      let m = (d.getMonth() + 1 + "").padStart(2, 0);
      let r = (d.getDate() + "").padStart(2, 0);
      let w = d.getDay();
      let date = `${y}-${m}-${r}`;
      dObj.date = date;
      dObj.w = week[w];
      return dObj;
    }
  },
  methods: {
    // 关闭对话框
    closmod() {
      this.examProp = false;
    },
    // 转换一下弹框时间
    timeEnter(data) {
      let d = new Date(data.start_time).getTime();
      let s = new Date(d);
      let m = s.getMonth() + 1;
      let r = s.getDate();
      let h = s.getHours();
      let mu = (s.getMinutes() + "").padStart(2, 0);
      this.dayStr = `${m}月${r}日${h}:${mu}`;
      this.modal1 = true;
    },
    // 万人模考排名
    dryrunBm(data) {
      let obj = {
        lb_id: data.lb_id,
        kmlb: data.kmlb,
        token: this.$store.state.$user.token,
        sj_id: data.id
      };
      this.$api.paper.dryrunBm(obj).then(res => {
        if (res.data.event != "100") return this.$Message.error(res.data.msg);
        this.gowrmk(); //刷新列表
        this.timeEnter(data);
      });
    },
    // start_count_down() {
    //   this.remaining_time -= 1000;
    //   this.timer = setInterval(() => {
    //     this.remaining_time -= 1000;
    //   }, 1000);
    // },

    // 智能组件事件
    znexam(data) {
      data.lb_id = this.router_params.lb;
      data.xlid = "";
      data.ctlx = "";
      data.sj_id = "";
      sessionStorage.setItem("znObj", JSON.stringify(data));
      this.$router.push({
        name: "paperExam",
        params: {
          product_id: this.router_params.lb,
          paper_id: data.kmlb,
          st_lx: "3"
        }
      });
    },

    // 每日一练
    tkDaily() {
      let obj = {
        xmlb_id: this.router_params.lb,
        token: this.$store.state.$user.token,
        date: this.day.date,
        week: this.day.w
      };
      this.$api.paper.newTkDaily(obj).then(res => {
        if (res.data.event == "100") {
          this.weekList = res.data.week;
          this.weekPaper = res.data.data;
          this.filtDate(res.data.data);
        }
      });
    },

    // 每日一练 换一批
    changePaper() {
      // console.log("换一批")
      let obj = {
        xmlb_id: this.router_params.lb,
        token: this.$store.state.$user.token,
        date: this.day.date,
        week: this.day.w,
        sj_done: this.idStr
      };
      this.$api.paper.upDaily(obj).then(res => {
        if (res.data.event == "100") {
          this.weekPaper = res.data.data;
          this.filtDate(res.data.data);
        }
      });
    },

    gopaper(item) {
      this.$router.push({
        name: "paperExam",
        params: {
          product_id: this.router_params.lb,
          paper_id: "5555",
          st_lx: "4"
        }
      });
    },

    // 对每日一练的数据进行过滤 只留下已做过得试卷的id
    filtDate(data) {
      let arr = [];
      data.forEach(item => {
        arr.push(item.id);
      });
      this.idStr = arr.join(",");
    },
    gost(row) {
      if (row.is_st == 0)
        return this.$Message.error({
          background: true,
          content: "暂无试题"
        });
      this.$router.push({
        name: "paperExam",
        params: {
          product_id: this.router_params.lb,
          paper_id: row.id,
          st_lx: "2"
        }
      });
    },
    dan(row) {
      this.$router.push({
        name: "paperExam",
        params: {
          product_id: this.router_params.lb,
          paper_id: row.id,
          st_lx: "1"
        }
      });
    },
    sortsdan(lx) {
      this.router_params.sort = lx;
      this.sorts(lx);
    },
    // 最新/最热
    sorts(data) {
      this.router_params.sort = data;
      this.get_list();
    },
    //分页相关
    on_page_change(page) {
      this.page_cmm.page = page;
      this.get_list();
    },
    on_size_change(size) {
      this.page_cmm.size = size;
      this.get_list();
    },
    refresh() {
      const query = Object.assign(
        {
          page: this.page_cmm.page
        },
        this.router_params
      );
      this.$router.push({
        path: this.$route.path,
        query
      });
    },
    // 章节练习试题
    getNewZjList() {
      let obj = {
        lb_id: this.router_params.lb,
        kmlb: this.router_params.kmlb,
        token: this.$store.state.$user.token,
        page: 1
      };
      this.$api.paper.newZjList(obj).then(res => {
        if (res.data.event == "100") {
          this.zjList = res.data.list;
        }
      });
    },
    // $param['lb'] = $this->input->post('lb');//项目 一建 二建 
    // $param['lb'] = 'jbu';//项目 一建 二建 
    // $param['kmlb'] = $this->input->post('kmlb');//科目 专业 id
    // $param['kmlb'] = 'j';//科目 专业 id
    // $param['sj_lx']= $this->input->post('sj_lx'); //试卷类型
    // $param['sj_lx']= '历年真题'; //试卷类型
    //list 相关
    get_list() {
      // if(!this.router_params.lb){
      //   return
      // }
      this.$api.paper
        .list({
          page: this.page_cmm.page,
          size: this.page_cmm.size,
          // lb: this.router_params.lb,
          // kmlb: this.router_params.kmlb,
          // sj_lx: this.router_params.genre, //转换字段,原为 sj_lx
          lb:'jbu',
          kmlb:'j',
          sj_lx:'历年真题',

          sort: this.router_params.sort
        })
        .then(rst => {
          this.list_data = rst.data.list || [];
          this.page_cmm.total = rst.data.total * 1;
        });
    },
    on_router_params_change(query) {
      this.router_params.lb = query.lb;
      this.router_params.kmlb = query.kmlb;
      this.router_params.genre = query.genre;
      this.page_cmm.page = 1;
      query.genre == "章节练习"
        ? this.getNewZjList()
        : query.genre == "智能练习"
          ? console.log("1122222")
          : query.genre == "每日一练"
            ? this.tkDaily()
            : this.get_list();
    },

    // gowrmks() {
    //   this.nowrmk = false;
    //   this.gowrmk()
    // },
    // submitPaper() {
    //   this.$router.go(0)
    //   this.fwTime();
    // },
    // // 万人模考列表
    // gowrmk() {
    //   // this.nowrmk = false;
    //   let obj = {
    //     token: this.$store.state.$user.token || "",
    //     lb_id: "4aud"
    //   };
    //   this.$api.paper.wrList(obj).then(rst => {
    //     let resD = rst.data.data;
    //     let wrLists = resD.map(item => {
    //       let startT = new Date(item.start_time);
    //       let endT = new Date(item.end_time);
    //       let sy = startT.getFullYear();
    //       let mounth = startT.getMonth() + 1;
    //       let day = startT.getDate();
    //       let sd = (startT.getDate() - 2 + "").padStart(2, 0);
    //       let sh = (startT.getHours() + "").padStart(2, 0);
    //       let sf = (startT.getMinutes() + "").padStart(2, 0);
    //       let ss = (startT.getSeconds() + "").padStart(2, 0);
    //       let eh = (endT.getHours() + "").padStart(2, 0);
    //       let ef = (endT.getMinutes() + "").padStart(2, 0);
    //       let dstr = `${mounth}月${day}日 ${sh}:${sf}-${eh}:${ef}`;
    //       let jss = `${sy}-${mounth}-${sd} ${sh}:${sf}:${ss}`;
    //       item.dayStrs = dstr;
    //       item.jsStart = jss;
    //       return item;
    //     });
    //     this.wrList = wrLists;
    //     sessionStorage.setItem("wrlists", JSON.stringify(this.wrList));
    //   });
    // },
    // // 正在考试
    // examIng(item) {
    //   this.$router.push({
    //     name: "wanExam",
    //     params: {
    //       sj_id: item.id
    //     }
    //   });
    // },
    // // 查看结果
    // lookEnd(item) {
    //   sessionStorage.setItem("phlists", "look");
    //   this.$router.push({
    //     name: "paperExam",
    //     params: {
    //       product_id: item.lb_id,
    //       paper_id: item.id,
    //       st_lx: "5"
    //     }
    //   });
    // },
    // // 没想的按钮点击对应的事件
    // choosBtn(item) {
    //   sessionStorage.setItem("wrObj", JSON.stringify(item));
    //   switch (item.zt) {
    //     case 0:
    //       // 报名
    //       this.dryrunBm(item);
    //       break;
    //     case 1: //已报名
    //       this.examTip(item); //弹框提示
    //       break;
    //     case 2: //正在考试
    //       this.examIng(item);
    //       break;
    //     case 3: //继续考试
    //       this.examIng(item);
    //       break;
    //     case 4: //查看结果
    //       this.lookEnd(item);
    //       // this.examIng(item);
    //       break;
    //     case 5: //未考试
    //       break;
    //     case "zt2": //考试已经开始，报名并参加
    //       this.dryrunBm(item);
    //       this.examIng(item);
    //       break;
    //   }
    // },
    // // 计时器显示与否
    // jsShow(str) {
    //   this.showTime.showJ = moment(str).isAfter(this.jsStartTime.jTime);
    //   this.showTime.showF = moment(str).isAfter(this.jsStartTime.fTime);
    //   this.showTime.showG = moment(str).isAfter(this.jsStartTime.gTime);
    //   this.showTime.showS = moment(str).isAfter(this.jsStartTime.sTime);
    //   this.showTime.showZ = moment(str).isAfter(this.jsStartTime.zTime);
    // },
    // // 是否开始考试了 当前时间处于考试之前才可以显示事件
    // isStartExam(str) {
    //   this.isStart.showJ = moment(str).isBefore(this.startExam.jTime);
    //   this.isStart.showF = moment(str).isBefore(this.startExam.fTime);
    //   this.isStart.showG = moment(str).isBefore(this.startExam.gTime);
    //   this.isStart.showS = moment(str).isBefore(this.startExam.sTime);
    //   this.isStart.showZ = moment(str).isBefore(this.startExam.zTime);
    // },
    // // 已报名的弹框提示文字赋值
    // examTip(data) {
    //   sessionStorage.setItem("AllPaper", 0);
    //   this.tipObj.kmTip = data.major;
    //   if (data.major == "经济") {
    //     this.tipObj.tipTime = this.startExam.jTime;
    //   } else if (data.major == "法规") {
    //     this.tipObj.tipTime = this.startExam.fTime;
    //   } else if (data.major == "管理") {
    //     this.tipObj.tipTime = this.startExam.gTime;
    //   } else if (data.major == "市政") {
    //     this.tipObj.tipTime = this.startExam.sTime;
    //   } else if (data.major == "建筑") {
    //     this.tipObj.tipTime = this.startExam.zTime;
    //   }
    //   this.examProp = true;
    // },
    // // 获取服务器时间
    // fwTime() {
    //   let obj = {
    //     token: this.$store.state.$user.token || ""
    //   };
    //   this.$api.paper.getTimeStamp(obj).then(res => {
    //     let dayD = new Date(res.data.service_time * 1000);
    //     let y = dayD.getFullYear();
    //     let m = (dayD.getMonth() + 1 + "").padStart(2, "0");
    //     let d = (dayD.getDate() + "").padStart(2, "0");
    //     let h = (dayD.getHours() + "").padStart(2, "0");
    //     let mm = (dayD.getMinutes() + "").padStart(2, "0");
    //     let s = (dayD.getSeconds() + "").padStart(2, "0");
    //     let str = `${y}-${m}-${d} ${h}:${mm}:${s}`;
    //     this.nowTime = str;
    //     this.jsShow(str);
    //     this.isStartExam(str);
    //     // this.gowrmk();
    //   });
    // },
    // wrNEW() {
    //   this.gowrmk();
    // }
  },
  beforeCreate() {
    this.$store.state.use_immersion_nav = false;
  },
  created() {
    // this.fwTime();
    
    // if (this.$route.query.wan == "1") {
    //   this.nowrmk = false;
    //   this.gowrmk();
    // }
    // if (sessionStorage.getItem("wrlists")) {
    //   // this.wrList = JSON.parse(sessionStorage.getItem("wrlists"));
    //   this.gowrmk();
    //   this.nowrmk = false;
    //   return;
    // }
    //init
    this.get_list();
    this.getNewZjList();
    // this.start_count_down()
  },
  mounted() {},
  beforeDestroy() {
    sessionStorage.removeItem("AllPaper");
    clearInterval(this.timer);
  },
  beforeRouteLeave(to, from, next) {
    if (to.name != "wanExam" && to.name != "paperExam") {
      sessionStorage.removeItem("wrlists");
    }
    next();
  },
  beforeRouteEnter(to, from, next) {
    if ($store.state.$user.token) return next();
    next("/login");
  }
};
